/**
 * Created by Administrator on 2016/8/11.
 */
define(["require",'config/appregister',"business/commpanyVideo/service/http","business/commpanyVideo/directive/upload","business/Companyinfor/service/chinese"],function(require,app,httpServices){
    app.controller('VideomodifyCtrl',function($scope,$interval,$stateParams,$state,$timeout){
        $scope.isDetailPage = true;
        $scope.isMainpage = false;
        $scope.goguiz=function(){
        	 $state.go("footerCtrl.helpDetailCtrl",{helpId:4,helpDetailId:1})
        }
        $scope.tags2 = [//ngInputTab自己写方法

        ];
        $scope.chapterCon=[];
        $scope.add = {
            title:"",
            labelIds:"你好",
            tcId:"",
            scId:"",
            thirdId:"",
            type:"",
            introduction:"",
            price:"",
            userId:"",
            copyright:"",
            visibility:"",
            previewPath:"",
            agreeFlag:1,
            jsonStr:[]
        }
        $scope.return=function(){
        	 $state.transitionTo("comIndexCtrl.resource", {}, {
                                    reload: false, inherit: false, notify: true
            });
            sessionStorage.ResourceTab = "1"
        }
        if($stateParams.courseId){
            var req ={
                type:"GET",
                uri:"get",
                data:{
                    courseId:$stateParams.courseId
                }
            };
            httpServices.getcourse(req).success(function(response){
                $scope.$apply(function(){
                    console.log(response.data);
                	$scope.hides=1;
                    $scope.add.title=response.data.course.title;
                    /*$scope.tags2=;*/
                    for(var i in response.data.course.labelList){
                        $scope.tags2.push(response.data.course.labelList[i].labelName)
                    }
                    $scope.Choice=response.data.category.topCategory.tcName;
                    $scope.add.tcId=response.data.category.topCategory.tcId;
                    $scope.Choice2=response.data.category.secCategory.scName;
                    $scope.add.scId=response.data.category.secCategory.scId;
                    if(response.data.category.thirdCategory){
                        $scope.Choice3=response.data.category.thirdCategory.dcName;
                        $scope.add.thirdId=response.data.category.thirdCategory.dcId;
                    }
                    $scope.add.introduction=response.data.course.introduction;
                    $scope.add.price=response.data.course.price;
                    $scope.teacheron=response.data.course.teacher.name;
                    $scope.add.visibility=response.data.course.visibility;//可见性
                    $scope.add.userId = response.data.course.createUserId;
                    $scope.courseIdsss = response.data.course.courseId;

                    if($scope.add.visibility == 0){
                        $scope.chec3=true;
                    }else{
                        $scope.chec4=true;
                    }
                    $scope.add.copyright=response.data.course.copyright;//版权
                    if($scope.add.copyright == 0){
                        $scope.chec2=true;
                    }else{
                        $scope.chec2=false;
                    }
                    $scope.uploadss=response.data.course.previewPath
              
                    $scope.add.previewPath=response.data.course.previewPath;
                    if(response.data.course.videoFlag==0){
                        $scope.isShowSend="course";
                        $scope.add.videoFlag = 0;
                        var couid = response.data.course.courseId;
                        var req ={
                            type:"GET",
                            uri:"getCourseCatalog",
                            data:{
                                courseId:couid
                            }
                        };
                        httpServices.getchapter(req).success(function(response){
                            $scope.$apply(function(){
                                $scope.add.videoFlag = 1;
                                if(response.data!=null){
                                    $scope.chapterCon=response.data;
                                }

                            })
                        });
                    }else{
                    	 $scope.add.videoFlag = 1;
                        $scope.isShowSend="vedio"
                         var couid = response.data.course.courseId;
                         var req ={
                            type:"GET",
                            uri:"getCourseCatalog",
                            data:{
                                courseId:couid
                            }
                        };
                        httpServices.getchapter(req).success(function(response){
                            $scope.$apply(function(){
                                if(response.data!=null){
                                    $scope.chapterCon=response.data;
                                }

                            })
                        });
                    }
                })
            });

        }
        $scope.saveTagsname=function(a){
            if($scope.tags2.length == 3){
                alert('最多只能添加三个标签哦，亲');
                return;
            }
            if($scope.tags2.indexOf(a) != -1){
                alert("标签不能相同哦")
            }else{
                $scope.tags2.push(a);
            }
        }
        $scope.deleteInput=function($index){
            $scope.tags2.splice($index,1);
        }

        $scope.hideNow=function(){
            $scope.isShowSend="send"
            $scope.chapterCon=[];
        }

        $scope.isShowSend = 'send';
        $scope.sendShow = function(item){
            $scope.isShowSend = item;
            if(item == 'vedio'){
                $scope.add.videoFlag = 1;
            }else if(item == 'course'){
                $scope.add.videoFlag = 0;
            }
        }
        $scope.teacheron="不限"
        $scope.showTwoDatas2=function(item){
            $scope.teacheron=item.name;
            $scope.add.userId = item.followUserId;

        }
        $scope.goTo = function(state){
            $state.go(state,{},{reload:true})
        }

        $scope.stus=[{"id":"1","name":"张三"},{"id":"2","name":"李四"}];
        $scope.change=function(item){
        }
        /*限制textatea的字数*/
        $scope.checkText = function () {
            $scope.length=$scope.add.introduction.length;
            if ($scope.add.introduction.length > 1000) {
                $scope.add.introduction = $scope.add.introduction.substr(0,1000);
                alert("输入内容超出限制了哦！");
            }
        };
        /*进度条*/
        function progress(){
            var vm = $scope.vm = {};
            vm.value = 50;
            vm.style = 'progress-bar-danger';
            vm.showLabel = true;
            vm.striped = true;
            $scope.selectValue = function (){
                /* console.log(vm.style);*/
            };
            var index = 0;
            var timeId = 500;
            $scope.count = function(){
                var start = $interval(function(){
                    vm.value =  ++index;
                    if (index > 99) {
                        $interval.cancel(start);
                    }
                    if (index == 60) {
                        index = 99;
                    }
                }, timeId);
            };
        }
        progress();
        /*判断标题名是否为空*/
        $scope.Red=2;
        $scope.add.title=function(name){
            $scope.unable="标题名不能为空"
            var value= /[@#\$%\^&\*]+/g;
            if( value.test(name)){
                $scope.unable="禁止输入非法字符";
                $scope.nameShow=1;
                $scope.Red=1;
            } else if(name){
                $scope.nameShow=2;
                $scope.Red=2;
            } else{
                $scope.nameShow=1;
                $scope.Red=1;
            }
        }
        /*动态添加删除文件夹*/
        $scope.creatCons=[];
        $scope.add=function(a){
            var obj = {
                id:0,
                name:a
            }///obj发送给后台即可文件夹分类
            $scope.creatCons.push(obj);
        }
        $scope.del=function(item,idx){
            $scope.creatCons.splice(idx,1);
            var req ={
                type:"DELETE",
                uri:"deleteCourseType",
                data:{
                    typeId:item.id
                }
            };
            httpServices.postcoursetype(req).success(function(response){
                $scope.$apply(function(){
                    /* console.log(response.code)*/
                })
            });
        }
        $scope.addcLass = function(item,index){
            item.isHover = !item.isHover;
        }
        /*获取课程分类*/
        $scope.showpropmt=function(){
            $scope.showpropmts=1;
            var req ={
                type:"GET",
                uri:"getCourseType",
                data:{}
            };
            httpServices.postcoursetype(req).success(function(response){
                $scope.$apply(function(){
                    $scope.creatCons=response.data;
                })
            });
        }
        //发送课程分类数据到后台
        $scope.saveCourseType=function(){
            var str =JSON.stringify($scope.creatCons);
            var req ={
                type:"POST",
                uri:"updateCourseType",
                data:{
                    jsonStr:str
                }
            };
            httpServices.postcoursetype(req).success(function(response){
                $scope.$apply(function(){
                    /* console.log(response.code)*///code=0代表成功
                    $scope.showpropmts=2;
                })
            });
        }
        /*动态删除添加章节目录*/
        //获取视频列表
        function getVedioList(){
            var req ={
                type:"GET",
                uri:"getList",
                data:{
                    all:true
                }
            };
            httpServices.getSameVideo(req).success(function(response){
                $scope.$apply(function(){
                    $scope.vedioList = response.list;
                })
            });
        }
        getVedioList();
        $scope.vidCOn=[
            {id:1,name:"电子技术"},
            {id:2,name:"半导体"},
            {id:3,name:"集成电路"},
            {id:4,name:"电子技术/半导体/集成电路"}
        ]
        $scope.arrVide=[]
        $scope.arrVideId=[];
        $scope.upVedio = [{
            one:{
                oneId:"",
                two:{
                    twoId:"",
                    three:{
                        threeId:"",
                        threeName:""
                    }
                }
            }
        }]
        $scope.saveVIdeo=function(item,index){
            $scope.videoId =item.videoId;
            $scope.videoCOn=item.videoName;
            $scope.bgs2=$scope.bgs=index;
        }
        $scope.addVdieoCon=function(){
            /*if($scope.videoId!=undefined){
             if($scope.arrVideId.indexOf($scope.videoId) != -1){

             }else{
             $scope.arrVide.push($scope.videoCOn);
             $scope.arrVideId.push($scope.videoId);

             }
             }*/
            if($scope.isShowSend == "vedio"){
                $scope.zhangNum = 0;
                $scope.jieNum = 0;
                if($scope.videoCOn && $scope.videoId){
                    $scope.chapterCon = [
                        {
                            chapterId:0,
                            chapterName:'第一章',
                            childChapterList:[{
                                chapterId:0,
                                chapterName:'第一节',
                                videoList:[{
                                    videoId:$scope.videoId,
                                    videoName:$scope.videoCOn
                                }]
                            }]
                        }
                    ]
                }else{
                    alert('请选择一个视频')
                }

            }else if($scope.isShowSend == "course"){
                $scope.chapterCon[$scope.zhangNum].childChapterList[$scope.jieNum].videoList[0] = {
                    videoId:$scope.videoId,
                    videoName: $scope.videoCOn
                };
            }

        }
        $scope.sav=function(index){
            $scope.videIndex=index;
            $scope.bgs2=index;
        }
        $scope.deleteData=function(index){
            $scope.bgs=-1;
            $scope.chapterCon[$scope.zhangNum].childChapterList[$scope.jieNum].videoList.splice(0,1)
            /*$scope.arrVide.splice(0,1)
             $scope.arrVideId.splice(0,1)*/
        }
        $scope.showVIdeoCon=function(index,index1){
            $scope.zhangNum = index1;
            $scope.jieNum = index;
            $scope.showV=1;
        }
        $scope.chapterCon=[];
        $scope.addCha=function(a,index){//新建章节
            var obj = {
                chapterId: 0,
                chapterName: a,
                childChapterList: [
                ]
            }
            $scope.chapterCon.push(obj);
            var height =document.querySelector("#heights").offsetHeight;
            /*           console.log(height)*/
            if(height>=712){
                $scope.showScroll=true;
            }
        }
        $scope.delete2=function(idx){//删除章节，删除当前章节数据即可
            $scope.chapterCon.splice(idx,1);
        }
        $scope.uploadId=0;
        $scope.addCHaps=function(a,$index){
        	if($scope.chapterCon[$scope.chapterCon.length-1].childChapterList.length != 0){
        		if($scope.chapterCon[$scope.chapterCon.length-1].childChapterList[$scope.chapterCon[$scope.chapterCon.length-1].childChapterList.length-1].videoList.length==0){
	  				alert("请完善上一节信息");
	  				return
	  			}
        	}
            $scope.uploadId++;
            $scope.index = $index;
            var obj = {
                chapterId:0,
                chapterNo:$index,
                chapterName:a,
                videoList: [

                ]
            }
            $scope.chapterCon[$scope.index].childChapterList.push(obj);

        }
        $scope.$on("a",function(a,b){

        })
        $scope.addVideo=function($index){
            var index = $index;
            $scope.chapterCon[$scope.index].childChapterList[index].videoList.push(obj);
        }

        $scope.delete3=function(index,index1){
            $scope.chapterCon[index1].childChapterList.splice(index,1);
        }

        /*加载数据*/
        /*课程分类数据*/
        /*一级分类*/
        function ONeClass(){
            var req ={
                type:"GET",
                uri:"getTopCategory",
                data:{}
            };
            httpServices.getcourseCata(req).success(function(response){
                $scope.$apply(function(){
                    $scope.couseONeData = response.data;
                })
            });
        }
        ONeClass();
        $scope.Choice2 = $scope.Choice3 = $scope.Choice = "请选择";
        $scope.showTwoData=function(item){
            $scope.Red2=2;
            $scope.Choice2 = $scope.Choice3 ="请选择";
            $scope.Choice = item.tcName;
            var id = item.tcId;//国内工程课程内容
            $scope.add.tcId = item.tcId;
            function oneData(){
                var req ={
                    type:"GET",
                    uri:"getSecondaryCategory",
                    data:{
                        tcId:id,
                        pageNo:1,
                        pageSize:100
                    }
                };
                httpServices.getcourseCata(req).success(function(response){
                    $scope.$apply(function(){
                        if(response){
                            $scope.Eligible =response.data.list;
                        }else{
                            return
                        }
                    })
                });
            }
           
            oneData();

        }
        $scope.showTwoDataTwo=function(item){
            $scope.Red2=2;
            var id = item.scId;
            $scope.add.scId = item.scId;
            $scope.Choice2 = item.scName;
            $scope.Choice3 ="请选择";
            var req ={
                type:"GET",
                uri:"getThirdCategory?scId="+id,
                data:{}
            };
            httpServices.getcourseCata(req).success(function(response){
                $scope.$apply(function(){
                    $scope.ThreeList = response.data;
                })
            });
        }

        //获取讲师列表
        function getFollowList(){
            var req ={
                type:"GET",
                uri:"getFollowList",
                data:{
                    all:true
                }
            };
            httpServices.getFollowList(req).success(function(response){
                $scope.$apply(function(){
                    $scope.follow = response.data.list;
                })
            });

        }
        getFollowList();

        $scope.ChangeCon=function(item){
            $scope.Red2=2;
            $scope.Choice3 = item.dcName;
            $scope.add.thirdId = item.dcId;
        }
        /*课程分类数据end*/
        $scope.check1=function(){
            $scope.chec=true;
            $scope.add.copyright=0;
            $scope.chec2=false;
            $scope.showRed=2;
        }
        $scope.check2=function(){
            $scope.chec=false;
            $scope.chec2=true;
            $scope.add.copyright=1;
            $scope.showRed=2;
        }
        $scope.check3=function(){
            $scope.chec3=true;
            $scope.chec4=false;
            $scope.showRed=2;
            $scope.add.visibility=1;
        }
        $scope.check4=function(){
            $scope.chec3=false;
            $scope.chec4=true;
            $scope.showRed=2;
            $scope.add.visibility=0;
        }
        $scope.check=false;
        $scope.agreeFlag = 0;
        $scope.argee=function(){
            $scope.check= !$scope.check;
            if($scope.agreeFlag == 0){
                $scope.agreeFlag = 1;
            }else if($scope.agreeFlag == 1){
                $scope.agreeFlag = 0;
            }
        }
        /*提交按钮*/

        $scope.$on('previewPath',function(a,b,c){
            $scope.add.previewPath = b;
        })
        $scope.Post=function() {
            if ( $scope.add.title == undefined ) {
                alert("请输入标题名");
                $scope.Red=1;
                $scope.nameShow=1;
            }else if($scope.Choice2 == "请选择" | $scope.Choice == "请选择"){
                alert("请选择课程分类")
                $scope.Red2=1;
            }else if(! $scope.chec && !$scope.chec2 ){
                alert("请选择版权")
                $scope.showRed=1;
            }else if( !$scope.chec3 && !$scope.chec4 ){
                alert("请选择费否可见")
                $scope.showRed=1;
            }else if(!$scope.check){
                alert("请同意《工程界相关服务条款》")
            }else if($scope.chapterCon.length==0){
            	alert("请输入章节内容")
            }else if($scope.chapterCon[0].chapterName==undefined){
            	alert("请完善章节信息")
            }else{
                var jsonStr = JSON.stringify($scope.chapterCon);
        
                var req ={
                    type:"POST",
                    uri:"update",
                    data:{
                        courseId:$scope.courseIdsss,
                        title:$scope.add.title,
                        labelIds:$scope.tags2.join(","),
                        tcId:$scope.add.tcId,
                        scId:$scope.add.scId,
                        thirdId:$scope.add.thirdId,
                        introduction:$scope.add.introduction,
                        price:$scope.add.price,
                        userId:$scope.add.userId,
                        copyright:$scope.add.copyright,
                        visibility:$scope.add.visibility,
                        previewPath:$scope.add.previewPath!=null?$scope.add.previewPath:"",
                        agreeFlag:$scope.agreeFlag,
                        jsonStr:jsonStr,
                        videoFlag:$scope.add.videoFlag
                    }
                };
                httpServices.getcourse(req).success(function(response){
                    $scope.$apply(function(){
                        if(response.code == 0){
                            $scope.sendSuccess = true;
                            $timeout(function(){
                                $state.go('eng.vedioDetails',{courseId:$scope.courseIdsss,hidePlay:1});
                            },1000);
                        }else{
                            alert(response.msg);
                        }
                    })
                });
            }
            $scope.closeSend = function(){
                $scope.sendSuccess = false;
            }


        }
        $scope.cancelVedio = function(){
        	 $scope.showV = 2;
            if($scope.chapterCon[$scope.zhangNum].childChapterList[$scope.jieNum].videoList.length == 0){
                $scope.chapterCon[$scope.zhangNum].childChapterList[$scope.jieNum].videoList.splice(0,1);
            }
           
        }
        $scope.saveVedio = function(){
            $scope.showV = 2;
        }




    })
});

